package com.sskj.givecoin.v.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.common.adapter.MyFragmentPagerAdapter;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.m.bean.FundBean;
import com.sskj.givecoin.p.activity.FundManagementPresenter;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.v.base.BaseActivity;
import com.sskj.givecoin.v.fragment.FundManagementFtagment;

import java.util.ArrayList;

import butterknife.BindView;

import static com.sskj.givecoin.common.Constants.INTENT_BALANCE;

/**
 * <pre>
 *     author : lee
 *     desc   : 基金管理
 *     version: 1.0
 * </pre>
 */
public class FundManagementActivity extends BaseActivity<FundManagementPresenter> {

    @BindView(R.id.fund_management_toolbar)
    ToolBarLayout mFundManagementToolbar;
    @BindView(R.id.fund_management_taday_output_value)
    TextView mFundManagementTadayOutputValue;
    @BindView(R.id.fund_management_taday_bouns_value)
    TextView mFundManagementTadayBounsValue;
    @BindView(R.id.fund_management_hold_value)
    TextView mFundManagementHoldValue;
    @BindView(R.id.fund_management_input)
    TextView mFundManagementInput;
    @BindView(R.id.fund_management_cumulative_bouns_value)
    TextView mFundManagementCumulativeBounsValue;
    @BindView(R.id.fund_tablayout)
    TabLayout mFundTablayout;
    @BindView(R.id.fund_viewpager)
    ViewPager mFundViewPager;
    /**
     * 用户钱包金额
     */
    private double mUserBalance;
    /**
     * 用户基金金额
     */
    private double mFundBalance;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fund_management;
    }

    @Override
    public FundManagementPresenter getPresenter() {
        return new FundManagementPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.requestData();
    }

    @Override
    protected void initView() {
        mFundManagementToolbar.setLeftButtonOnClickLinster(v -> finish());
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<Fragment> fragments = new ArrayList<>();
        titles.add(App.INSTANCE.getString(R.string.strFundManagementActivity200));
        titles.add(App.INSTANCE.getString(R.string.strFundManagementActivity201));
        fragments.add(FundManagementFtagment.newInstence(true));
        fragments.add(FundManagementFtagment.newInstence(false));

        LinearLayout linearLayout = (LinearLayout) mFundTablayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(getResources().getDrawable(R.drawable.shape_rectangle_gray));
        linearLayout.setDividerPadding(DensityUtil.dp2px(16));

        mFundViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), titles, fragments));
        mFundViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                ((FundManagementFtagment)fragments.get(position)).upDate();
            }
        });
        mFundTablayout.setupWithViewPager(mFundViewPager);
    }

    @Override
    protected void initData() {
        ClickUtil.click(mFundManagementInput, () -> {
            startActivity(new Intent(mActivity, FundTransferActivity.class).putExtra(INTENT_BALANCE, mUserBalance).putExtra(Constants.INTENT_FUND_BALANCE, mFundBalance));
        });

    }

    public void onDataSuccess(FundBean fundBean) {
        mUserBalance = fundBean.getYue();
        mFundBalance = fundBean.getWallbf();
        mFundManagementTadayBounsValue.setText(String.valueOf(fundBean.getFund()));
        mFundManagementTadayOutputValue.setText(String.valueOf(fundBean.getFrostbf()));
        mFundManagementCumulativeBounsValue.setText(String.valueOf(fundBean.getWallfund()));
        mFundManagementHoldValue.setText(String.valueOf(fundBean.getWallbf()));
    }
}
